package com.vivo.util;

import com.vivo.exception.DToolCheckException;

/**
 * Description: Tool for Print Information, Check Expression, and Throw
 * Exceptions, etc.<BR>
 * 
 * @author Gaobo
 * @ChangeLog
 *         <ul>
 *         <li>2008-01-29 create by gaobo</li>
 *         <li>2008-01-30 improve by gaobo</li>
 *         </ul>
 */
public class DTool {

	/** GLOBAL D Tools in ERROR level */
	public static final DTool e = new DTool(DTool.ERROR);
	/** GLOBAL D Tools in DEBUG level */
	public static final DTool d = new DTool(DTool.DEBUG);
	/** GLOBAL D Tools in INFO level */
	public static final DTool i = new DTool(DTool.INFO);

	public static final int ERROR = 1;
	public static final int DEBUG = 2;
	public static final int INFO = 3;

	private int level = 3;

	public DTool() {
	}

	public DTool(int level) {
		this.level = level;
	}

	/*
	 * checks
	 */

	/**
	 * Throw new InvalidParameterException if boolean expression is false.
	 * 
	 * @param expression
	 */
	public void check(boolean expression) {
		if (expression) {
			throw new DToolCheckException();
		}
	}

	/**
	 * Print Message if expression is true.And in enough level.
	 * 
	 * @param expression
	 * @param message
	 */
	public void check(boolean expression, String message) {
		if (expression) {
			switch (level) {
			case DTool.ERROR:
				error(message);
				break;
			case DTool.DEBUG:
				debug(message);
				break;
			case DTool.INFO:
				info(message);
				break;
			default:
				System.out.println("D check: " + message);
			}
		}
	}

	/**
	 * Throw Exception if expression is true.
	 * 
	 * @param expression
	 * @param ex
	 */
	public void check(boolean expression, RuntimeException ex) {
		if (expression) {
			throw ex;
		}
	}

	/*
	 * assert
	 */

	/**
	 * Throw new InvalidParameterException if boolean expression is false.
	 * 
	 * @param expression
	 */
	public void assertit(boolean expression) {
		if (!expression) {
			throw new AssertionError();
		}
	}

	/**
	 * Print Message if expression is true.And in enough level.
	 * 
	 * @param expression
	 * @param message
	 */
	public void assertit(boolean expression, String message) {
		if (!expression) {
			switch (level) {
			case DTool.ERROR:
				error(message);
				break;
			case DTool.DEBUG:
				debug(message);
				break;
			case DTool.INFO:
				info(message);
				break;
			default:
				System.out.println("D check: " + message);
			}
		}
	}

	/**
	 * Throw Exception if expression is true.
	 * 
	 * @param expression
	 * @param ex
	 */
	public void assertit(boolean expression, RuntimeException ex) {
		if (!expression) {
			throw ex;
		}
	}

	/*
	 * Debug
	 */

	/**
	 * Print message if level >= INFO.
	 * 
	 * @param message
	 */
	public void info(String message) {
		if (INFO <= level && null != message) {
			System.out.println("D Info: " + message);
		}
	}

	/*
	 * Debug
	 */

	/**
	 * Print message if level >= Debug.
	 * 
	 * @param message
	 */
	public void debug(String message) {
		debug(message, null);
	}

	/**
	 * Throw t if level >= Debug.
	 * 
	 * @param t
	 */
	public void debug(Throwable t) {
		debug(null, t);
	}

	/**
	 * Print message and throw t if level >= Debug.
	 * 
	 * @param message
	 * @param t
	 */
	public void debug(String message, Throwable t) {
		if (DEBUG <= level) {
			if (null != message)
				System.out.println("D debug: " + message);
			if (null != t)
				t.printStackTrace();
		}
	}

	/*
	 * Error
	 */

	/**
	 * Print message if level >= Debug.
	 * 
	 * @param message
	 */
	public void error(String message) {
		error(message, null);
	}

	/**
	 * Throw t if level >= Debug.
	 * 
	 * @param t
	 */
	public void error(Throwable t) {
		error(null, t);
	}

	/**
	 * Print message and throw t if level >= Debug.
	 * 
	 * @param message
	 * @param t
	 */
	public void error(String message, Throwable t) {
		if (ERROR <= level) {
			if (null != message)
				System.out.println("D error: " + message);
			if (null != t)
				t.printStackTrace();
		}
	}

	/**
	 * Execute
	 * 
	 * @return
	 */
	public void debugExecute(boolean expression, Func func) {
		if (DEBUG <= level) {
			func.exec();
		}
	}

	public interface Func {
		public void exec();
	}

	/*
	 * Getter and Setters
	 */

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

}
